import _mysql

class db:
	def __init__(self, hostdb, portdb, namedb, userdb, passdb):
		self.db = _mysql.connect(host=hostdb, user = userdb,
					passwd = passdb, port = portdb, db=namedb)
		
	def __del():
		self.db.close()
		
	def postText(self, title, text):
		self.db.query("""SELECT max(id) from paste""")
		r = self.db.store_result()
		(row,)	= r.fetch_row()
		(str_id,)	= row
		id = int(str_id) + 1
		self.db.query(
		"""INSERT into paste(id, title, text) values(%d,"%s", "%s")""" 
		 % (id, _mysql.escape_string(title), _mysql.escape_string(text)))
		
		return id
							
	def getText(self, id):
		self.db.query("""SELECT title, text,language from paste where id=%s """ % id)
		r = self.db.store_result()
		(row,)	= r.fetch_row()
		#~ print row
		(title, text, language)	= row

		return row
	
	def delText(self, id):
		self.db.query("""DELETE from paste where id=%s """ % id)
		
		
